package cn.eng.org.service.impl;

import cn.eng.basic.exception.BusinessException;
import cn.eng.basic.util.BaiduAuditUtils;
import cn.eng.org.domain.Employee;
import cn.eng.org.domain.Shop;
import cn.eng.org.domain.ShopAuditLog;
import cn.eng.org.dto.ShopDto;
import cn.eng.org.mapper.EmployeeMapper;
import cn.eng.org.mapper.ShopAuditLogMapper;
import cn.eng.org.mapper.ShopMapper;
import cn.eng.org.service.IShopService;
import cn.eng.basic.service.impl.BaseServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    @Autowired
    private JavaMailSender javaMailSender;

    @Override
    public void settlement(Shop shop) {
        //1.先添加t_employee => 自增长id
        //2.将自增长id设置到shop对象
        //3.再添加t_shop => 自增长id
        //4.将shop的自增长id设置到employee对象
        //5.修改t_employee

        //1.空值校验，前端校验容易被禁用掉，需要后端校验
        //判断如果店铺名称为空 抛出异常
        if (StringUtils.isEmpty(shop.getName())){
            throw  new BusinessException("店铺名称不能为空!");
        }
        //判断如果地址为空,抛出异常
        if (StringUtils.isEmpty(shop.getAddress())){
            throw  new BusinessException("店铺地址不能为空!");
        }
        if(!BaiduAuditUtils.TextCensor(shop.getName())){
            throw new BusinessException("店铺名称不合法！");
        }

        if(!StringUtils.isEmpty(shop.getLogo())
                && !BaiduAuditUtils.ImgCensor
                ("http://123.207.27.208"+shop.getLogo())){
            throw new BusinessException("店铺logo不合法！");
        }

        //2.校验当前店铺是否入驻：只要名称和地址是一样的
        Shop dbShop = shopMapper.findByNameAndAddress(shop);
        if(dbShop != null){
            throw new BusinessException("你的店铺已经入驻，请直接登录!如果忘记密码，请联系管理员!!!");
        }
        //获取employee 的对象
        Employee employee = shop.getAdmin();
        //给状态设置默认值 同时能获取employee 的ID
        employee.setState(0);//默认禁用
        //保存店铺管理员信息t_employee
        employeeMapper.save(employee);
        //把employee的ID设置到t_shop里面
        shop.setAdminId(employee.getId());
        //保存店铺信息t_shop
        shopMapper.save(shop);
        //获取t_shop的id 同时给设置给employee 对象
        employee.setShop_id(shop.getId());
        //将店铺的id更新到t_employee中
        employeeMapper.update(employee);
    }

    /**
     * 审核通过的逻辑
     * @param log
     * @throws MessagingException
     */
    @Override
    public void pass(ShopAuditLog log) throws MessagingException {
        //1.改店铺的状态: shop state 1(待审核)->2(待激活)
        shopMapper.updateState(log.getShopId(),2);
        //2.添加审核日志
        log.setState(2);
        shopAuditLogMapper.save(log);
            //3.发送邮件：告诉店铺管理员激活邮件使用
            //创建复杂邮件对象
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            //发送复杂邮件的工具类
            //true - 能够添加附件
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
            helper.setFrom("2553663780@qq.com");
            helper.setSubject("店铺邮件");
            //true - 能够再内容中编写html标签 - 会解析
            Shop shop = shopMapper.loadById(log.getShopId());
            helper.setText("<h1>你的店铺已经注册!!!</h1><img src='http://123.207.27.208"+shop.getLogo()
                            +"' ><a href='http://127.0.0.1:8080/shop/active/"+shop.getId()+"'>点击该链接激活</a>",
                    true);
            Employee employee = employeeMapper.loadById(shop.getAdminId());
            //收件人
            helper.setTo(employee.getEmail());
            javaMailSender.send(mimeMessage);
        }

    /**
     * 审核驳回的逻辑
     * @param log
     * @throws MessagingException
     */
    @Override
    public void reject(ShopAuditLog log) throws MessagingException {
//1.改店铺的状态: shop state 1(待审核)->4(驳回)
        shopMapper.updateState(log.getShopId(),4);
        //2.添加审核日志
        log.setState(4);
        shopAuditLogMapper.save(log);
        //3.发送邮件：告诉店铺管理员激活邮件使用
        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        //true - 能够添加附件
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("2553663780@qq.com");
        helper.setSubject("店铺审核失败");
        //true - 能够再内容中编写html标签 - 会解析
        Shop shop = shopMapper.loadById(log.getShopId());
        helper.setText("<h1>你的店铺入驻失败，请修改信息之后重新入驻</h1>",
                true);
        Employee employee = employeeMapper.loadById(shop.getAdminId());
        //获取收件人的邮箱
        helper.setTo(employee.getEmail());
        //发送
        javaMailSender.send(mimeMessage);
    }

    /**
     * 柱状图 查询每一个状态的个数
     * @return
     */
    @Override
    public List<ShopDto> echarts() {
        return shopMapper.echarts();
    }

    /**
     * 店铺激活业务激活
     * @param id
     * @throws MessagingException
     */
    @Override
    public void active(Long id) throws MessagingException {
        //1.改店铺的状态: shop state 1(待审核)->2(待激活)
        Shop shop = shopMapper.loadById(id);
        shopMapper.updateState(id,3);
        
        //2.添加审核日志
      /*  log.setState(3);
        shopAuditLogMapper.save(shop);
        //3.发送邮件：告诉店铺管理员激活邮件使用
        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        //true - 能够添加附件
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("2553663780@qq.com");
        helper.setSubject("店铺激活邮件");
        //true - 能够再内容中编写html标签 - 会解析
        Shop shop = shopMapper.loadById(log.getShopId());
        helper.setText("<h1>你的店铺已经激活成功啦!!!",
                true);
        Employee employee = employeeMapper.loadById(shop.getAdminId());
        //收件人
        helper.setTo(employee.getEmail());
        javaMailSender.send(mimeMessage);*/
    }

}

